'''
Created on Sep 28, 2010

@author: morgan
'''
from string import Template
import unittest
import kml

print kml

class UnitTests(unittest.TestCase):
    #--------------------------------------------------------------------------
    # Setup and utilities 
    #--------------------------------------------------------------------------
    
    def setUp(self):
        self.kml = kml.kml()
        self.verbose = False

    def tearDown(self):
        del self.kml
        
    @property
    def verbose(self):
        return self._verbose
    
    @verbose.setter
    def verbose(self, value):
        if type(value) == bool:
            self._verbose = value
        else:
            raise TypeError("verbose must be a boolean")
        
    def assertAssignRaises(self, exception, KMLObject, attribute, value):
        """ 
        This is roughly equivalent to:
        self.assertRaises(exception, KMLObject.attribute = value)
        except, of course, you can't do that. ;)
        """
        classname = KMLObject.__class__.__name__
        exceptname = exception.__name__
        try:
            KMLObject.__setattr__(attribute, value)
        except exception:
            pass
        except Exception as inst:
            realexception = inst.__class__.__name__
            msg=Template("Exception $r, not $e occurred for illegal $c.$a value")
            msg=msg.substitute(r=realexception, e=exceptname, c=classname, a=attribute)
            self.fail(msg)
        else:
            msg=Template("Exception $e did not occur for illegal $c.$a value")
            msg=msg.substitute(e=exceptname, c=classname, a=attribute)
            self.fail(msg)
            
    def assertAssignSuccessful(self, KMLObject, attribute, value):
        KMLObject.__setattr__(attribute, value)
        self.assertEqual(KMLObject.__getattribute__(attribute), value)
           
        
    def cloneTest(self, KMLObject, debug=False):
        """ 
        tests the ability of a KMLObject create an XML node and to then 
        parse it back.  Each object is then converted to a string (XML) and
        compared.  The two strings should be equal, otherwise an exception is
        raised.
        
        This test is a sort-of acid test. For a fully-filled out object, this
        test will hit every single line of code in the KMLObject-derived class.
        The only exception to this rule is if, somehow, you've got data leaking
        in from another instance of this class or a predecessor.  I'm not sure
        under which conditions this occurs and believe that it may be a Python
        bug; however, since I can't (yet) reproduce the scenario in a simple
        isolated test case, I'm not officially calling it a Python bug.  I
        consider it more likely that it is a gap in my understanding of how
        Python handles some aspects of inheritance in new-style classes
        """ 
        source = KMLObject
        target = KMLObject.__class__()
        target.parseKMLNode(source.toXmlNode(self.kml))
        if debug:
            print("\n")
            print("*** source:")  
            print(source.toStr(self.kml, True).replace("\t", "  "))
            print("*** target:")
            print(target.toStr(self.kml, True).replace("\t", "  "))
        self.assertEqual(source.toStr(self.kml, True), 
                         target.toStr(self.kml, True))
        
    def modeTest(self, mode, testvalue):
        """ test a mode.  testvalue needs to be a non-default value """
        self.assertEqual(mode.value, mode.__class__.default)
        self.assertNotEqual(mode.validModes, [])
        for value in mode.validModes:
            self.assertEqual(mode.__getattribute__(value), value)
        mode.actualMode = testvalue
        self.assertRaises(ValueError, mode.__class__, "bogus")
        self.assertEqual(mode.actualMode, mode.__getattribute__(testvalue))
        self.assertNotEqual(mode.defaultMode, mode.__getattribute__(testvalue))
        self.cloneTest(mode, self.verbose)
    
    #--------------------------------------------------------------------------
    # Base classes
    #--------------------------------------------------------------------------
        
    def testColor(self):
        color = kml.Color(255, 127, 63, 1) # nice darkish blue!
        self.assertEqual(color.alpha, 0xFF)
        self.assertEqual(color.blue, 0x7F)
        self.assertEqual(color.green, 0x3F)
        self.assertEqual(color.red, 0x01)
        self.assertEqual(color.value, 0xff7f3f01)
        color2 = kml.Color()
        color2.value = 0xff7f3f01
        self.assertEqual(color.value, color2.value)

    #--------------------------------------------------------------------------
    # Enumerated classes
    #--------------------------------------------------------------------------
    def testAltitudeMode(self):
        self.modeTest(kml.altitudeMode(), "clampToGround")

    def testDisplayMode(self):
        self.modeTest(kml.displayMode(), "hide")
            
    def testExtrude(self):
        extrude = kml.extrude()
        self.assertFalse(extrude.value)
        extrude.value = True
        self.assertTrue(extrude.value)
        newExtrude = kml.extrude()
        newExtrude.parseKMLNode(extrude.toXmlNode(self.kml))
        self.assertNotEqual(extrude.toStr(self.kml, True),"")
        self.assertEqual(extrude.toStr(self.kml, True), 
                         newExtrude.toStr(self.kml, True))
        
    def testColorMode(self):
        self.modeTest(kml.colorMode(), "random")
        
    def testRefreshMode(self):
        self.modeTest(kml.refreshMode(), "onExpire")
    
    def testViewRefreshMode(self):
        self.modeTest(kml.viewRefreshMode(), "onStop") 
        
    #--------------------------------------------------------------------------
    # Data classes
    #--------------------------------------------------------------------------
    
    def testCoordinates(self):
        coords1 = kml.coordinates()
        self.assertRaises(TypeError, kml.Coordinate, 'foo', 2, 3)
        self.assertRaises(TypeError, kml.Coordinate, 1, 'bar', 3)
        self.assertRaises(TypeError, kml.Coordinate, 1, 2, 'baz')
        self.assertRaises(Exception, kml.Coordinate, coords="Foo")
        self.assertRaises(ValueError, kml.Coordinate, 190, 2, 3)
        self.assertRaises(ValueError, kml.Coordinate, 1, -290, 3)
        # this should not fail
        kml.Coordinate(1, 2, 390)
        coord1 = kml.Coordinate(1,2,3)
        coord2 = kml.Coordinate()
        coord2.latitude = 2
        coord2.longitude = 1
        coord2.altitude = 3
        self.assertEqual(str(coord1), str(coord2))
        coord2.longitude = 4
        coord2.latitude = 5
        coord2.altitude = 6
        coords1.append(coord1)
        coords1.append(coord2)
        self.cloneTest(coords1, self.verbose)
        
    def testLocation(self):
        loc = kml.Location(1,2,3)
        coord = kml.Coordinate(1,2,3)
        loc2 = kml.Location(coords=coord)
        self.assertEqual(str(loc), str(loc2))
        self.cloneTest(loc2, self.verbose)
        
    def testOrientation(self):
        orient = kml.Orientation(0, 60, 90)
        self.cloneTest(orient, self.verbose)
        
    def testLink(self):
        time=10
        refresh="onInterval"
        viewRefresh="onRequest"
        scale=0.75
        pretty=True
        link = kml.Link(id="foo", href="http://example.com/kml", 
                        refreshMode=refresh, 
                        refreshInterval=time,
                        viewRefreshMode=viewRefresh, 
                        viewRefreshInterval=time, 
                        viewBoundScale=scale)
        self.assertEqual(link.refreshMode, refresh)
        self.assertEqual(link.viewRefreshMode, viewRefresh)
        self.assertEqual(link.viewBoundScale, scale)
        self.assertRaises(TypeError, kml.Link, refreshMode=1)
        self.assertRaises(ValueError, kml.Link, refreshMode="foo")
        self.assertRaises(ValueError, kml.Link, refreshInterval=-1)
        self.assertRaises(TypeError, kml.Link, refreshInterval="foo")
        self.assertRaises(TypeError, kml.Link, viewRefreshMode=1)
        self.assertRaises(ValueError, kml.Link, viewRefreshMode="foo")
        self.assertRaises(ValueError, kml.Link, viewRefreshInterval=-1)
        self.assertRaises(TypeError, kml.Link, viewRefreshInterval="foo")
        self.cloneTest(link, self.verbose)
        
    def testIcon(self):
        href="root:///icons/palette-3.png"
        x=96
        y=128
        h=32
        w=32
        icon = kml.Icon(href="root:///icons/palette-3.png", x=x, y=y,
                        height=h, width=w)
        self.assertEqual(icon.x, x)
        self.assertEqual(icon.y, y)
        self.assertEqual(icon.width, w)
        self.assertEqual(icon.height, h)
        icon=kml.Icon()
        icon.href=href
        icon.x=x
        icon.y=y
        icon.height=h
        icon.width=w
        self.assertEqual(icon.x, x)
        self.assertEqual(icon.y, y)
        self.assertEqual(icon.width, w)
        self.assertEqual(icon.height, h)
        self.cloneTest(icon, self.verbose)

    def testScale(self):
        f1 = 0.5
        f2 = 0.6
        f3 = 0.7
        scale = kml.Scale(x=f1, y=f2, z=f3)
        self.assertEqual(scale.x, f1)
        self.assertEqual(scale.y, f2)
        self.assertEqual(scale.z, f3)
        self.assertRaises(TypeError, kml.Scale, x="foo")
        self.assertRaises(TypeError, kml.Scale, y="foo")
        self.assertRaises(TypeError, kml.Scale, z="foo")
        self.assertRaises(ValueError, kml.Scale, x=-1)
        self.assertRaises(ValueError, kml.Scale, y=-1)
        self.assertRaises(ValueError, kml.Scale, z=-1)
        scale=kml.Scale()
        self.assertEqual(scale.x,0)
        scale.x = f1
        scale.y = f2
        scale.z = f3
        self.assertEqual(scale.x, f1)
        self.assertEqual(scale.y, f2)
        self.assertEqual(scale.z, f3)
        self.cloneTest(scale, self.verbose)

    def testAlias(self):
        s="foo"
        t="bar"
        alias = kml.Alias(s, t)
        self.assertEqual(alias.sourceHref, s)
        self.assertEqual(alias.targetHref, t)
        alias=kml.Alias()
        alias.sourceHref = s
        alias.targetHref = t
        self.assertEqual(alias.sourceHref, s)
        self.assertEqual(alias.targetHref, t)
        #alias2=kml.Alias()
        #self.cloneTest(alias, alias2)
        self.cloneTest(alias, self.verbose)
        
    def testResourceMap(self):
        d=dict(foo="bar", baz="banf", fubar="foobar")
        aliases=[]
        for key in d.keys():
            aliases.append(kml.Alias(key, d[key]))
        map=kml.ResourceMap()
        for alias in aliases:
            map.addAlias(alias)
        self.assertEqual(aliases, map.Aliases)
        
        map.removeAlias(aliases[1])
        self.assertFalse(aliases[1] in map.Aliases)
       
        map.clearAliases()
        self.assertFalse(map.Aliases)
        self.cloneTest(map, self.verbose)
    
    def testHotSpot(self):
        x = 0.25
        y = 0.35
        hotspot = kml.hotSpot(x, y)
        self.assertEqual(hotspot.xfraction, True)
        self.assertEqual(hotspot.yfraction, True)
        self.assertEqual(hotspot.x, x)
        self.assertEqual(hotspot.y, y)
        x = 5
        y = 3
        hotspot.xfraction = False
        hotspot.x = x
        hotspot.yfraction = False
        hotspot.y = x
        self.assertEqual(hotspot.xunits, "insetPixels")
        self.assertEqual(hotspot.yunits, "insetPixels")
        self.assertRaises(ValueError, kml.hotSpot, 2)
        self.assertRaises(ValueError, kml.hotSpot, 1,2)
        self.assertRaises(TypeError, kml.hotSpot, 0.25,0.25,xunits="insetPixels", 
                          yunits="insetPixels")
        self.assertRaises(TypeError, kml.hotSpot, xunits="insetPixels")
        self.assertRaises(ValueError, kml.hotSpot, xunits="foo")
        self.cloneTest(hotspot, self.verbose)
        
    #--------------------------------------------------------------------------
    # Styles
    #--------------------------------------------------------------------------
        
    def testBalloonStyle(self):
        id = "foo"
        bgColor = 0xff7f3f01
        textColor = kml.Color(255, 255, 255, 255)
        text = "Hello, World."
        DisplayMode = kml.displayMode("default")
        style = kml.BalloonStyle(id, bgColor, textColor, text, DisplayMode)
        self.assertEqual(style.id, id)
        self.assertEqual(style.bgColor, bgColor)
        self.assertEqual(style.textColor, int(str(textColor),16))
        self.assertEqual(style.text, text)
        self.assertEqual(style.displayMode, DisplayMode.value)
        self.cloneTest(style, self.verbose)
    
    def testLineStyle(self):
        id = "foo"
        width=4
        darkBlue = kml.Color(255, 127, 63, 1)
        colorMode = kml.colorMode("normal")
        style = kml.LineStyle(id, width, color=darkBlue, colorMode=colorMode)
        self.assertEqual(style.id, id)
        self.assertEqual(style.color, darkBlue.value)
        self.assertEqual(style.colorMode, colorMode.value)
        self.assertEqual(style.width, width)
        self.cloneTest(style, self.verbose)
    
    def testIconStyle(self):
        id = "foo"
        icon = kml.Icon(href="1.png")
        scale=0.75
        heading=0
        hotSpot = kml.hotSpot(0.25, 0.25)
        style = kml.IconStyle(id, scale, heading, icon)
        style.hotSpot=hotSpot
        self.assertEqual(style.Icon, icon)
        self.assertEqual(style.scale, scale)
        self.assertEqual(style.heading, heading)
        self.assertEqual(style.hotSpot, hotSpot)
        self.cloneTest(style, self.verbose)
    
    #--------------------------------------------------------------------------
    # Geometry
    #--------------------------------------------------------------------------
    
    def testLineString(self):
        id = "foo"
        coords = kml.coordinates()
        for n in range(0,10):
            coords.append(kml.Coordinate(n, n+1 ,n+2))
        linestring = kml.LineString(id=id)
        linestring.extrude = True
        linestring.tessellate = True
        linestring.coordinates = coords
        self.assertEqual(linestring.extrude, True)
        self.assertEqual(linestring.tessellate, True)
        self.assertEqual(linestring.coordinates, coords.value)
        self.cloneTest(linestring, self.verbose)
        del linestring, coords
        
    def testLinearRing(self):
        id = "foo"
        openCoords = kml.coordinates()
        for n in range(0,4):
            openCoords.append(kml.Coordinate(n, n+1, n+2))
        closedCoords = kml.coordinates()
        for n in range(0,4):
            closedCoords.append(kml.Coordinate(n, n*2, n*3))
        closedCoords.append(closedCoords.value[0])
        linearRing = kml.LinearRing(coordinates=openCoords)
        self.assertRaises(Exception,linearRing.toXmlNode, self.kml)
        linearRing.coordinates = closedCoords
        self.assertEqual(linearRing.coordinates, closedCoords.value)
        self.assertEqual(linearRing.extrude, False)
        self.assertEqual(linearRing.tessellate, False)
        self.assertEqual(linearRing.altitudeMode, kml.altitudeMode.default)
        linearRing.extrude = kml.extrude(True)
        linearRing.tessellate = kml.tessellate(True)
        self.assertEqual(linearRing.tessellate, True)
        self.assertEqual(linearRing.extrude, True)
        linearRing.extrude = False
        linearRing.tessellate = False
        self.assertEqual(linearRing.extrude, False)
        self.assertEqual(linearRing.tessellate, False)
        self.assertRaises(TypeError, kml.LinearRing, extrude=1)
        self.assertRaises(TypeError, kml.LinearRing, tessellate=1)
        linearRing.extrude = True
        linearRing.tessellate = True
        self.cloneTest(linearRing, self.verbose)
        
    def testPoint(self):
        pt1 = kml.Point("", 1,2,3)
        self.assertEqual(pt1.longitude, 1)
        self.assertEqual(pt1.latitude, 2)
        self.assertEqual(pt1.altitude, 3)
        pt2 = kml.Point(coordinates=kml.Coordinate(1,2,3))
        self.assertEqual(pt2.longitude, 1)
        self.assertEqual(pt2.latitude, 2)
        self.assertEqual(pt2.altitude, 3)
        pt3 = kml.Point()
        pt3.longitude = 1
        pt3.latitude = 2
        pt3.altitude = 3
        self.assertEqual(pt3.longitude, 1)
        self.assertEqual(pt3.latitude, 2)
        self.assertEqual(pt3.altitude, 3)
        self.assertRaises(TypeError, kml.Point, "", "10 W", 2, 3)
        self.assertRaises(TypeError, kml.Point, "", 1, "10 N", 3)
        self.assertRaises(TypeError, kml.Point, "", 1, 2, None)
        self.cloneTest(pt1, self.verbose)
        
    def testModel(self):
        model = kml.Model(id="foo")
        model.altitudeMode = kml.altitudeMode.clampToGround
        model.Location = kml.Location(1, 2, 3)
        model.Orientation = kml.Orientation(0, 45, 90)
        model.Scale = kml.Scale(1, 1, 1)
        model.Link = "models/model.dae"
        model.ResourceMap = kml.ResourceMap()
        model.ResourceMap.addAlias(kml.Alias("models/model.dae", 
                                             "/nas/Models/test/model.dae"))
        self.assertEqual(model.altitudeMode, kml.altitudeMode.clampToGround)
        self.assertEqual(model.Location.longitude, 1)
        self.assertEqual(model.Location.latitude, 2)
        self.assertEqual(model.Location.altitude, 3)
        self.assertEqual(model.Orientation.heading, 0)
        self.assertEqual(model.Orientation.tilt, 45)
        self.assertEqual(model.Orientation.roll, 90)
        self.assertEqual(model.Scale.x, 1)
        self.assertEqual(model.Scale.y, 1)
        self.assertEqual(model.Scale.z, 1)
        self.assertEqual(model.Link.href, "models/model.dae")
        self.assertEqual(model.ResourceMap.Aliases[0].sourceHref, 
                         "models/model.dae")
        self.assertEqual(model.ResourceMap.Aliases[0].targetHref,
                         "/nas/Models/test/model.dae")
        self.cloneTest(model, self.verbose)
        
    
        
        
        
        
    #--------------------------------------------------------------------------
    # Time
    #--------------------------------------------------------------------------
    
    def testTimeStamp(self):
        timestr = "2010-10"
        timestamp = kml.TimeStamp("", 2010, 10)
        self.assertEqual(str(timestamp), timestr)
        del timestamp
        timestamp = kml.TimeStamp()
        timestamp.parseDate(timestr)
        self.assertEqual(str(timestamp), timestr)
        self.assertEqual(timestamp.hour, None)
        self.assertEqual(timestamp.minute, None)
        self.assertEqual(timestamp.second, None)
        self.assertEqual(timestamp.month, 10)
        timestamp.day = 3
        timestr = "-".join([timestr, str(timestamp.day).zfill(2)])
        self.assertEqual(str(timestamp), timestr)
        timestamp.hour=0
        self.assertEqual(timestamp.hour, 0)
        self.assertEqual(timestamp.minute, 0)
        self.assertEqual(timestamp.second, 0)
        timestamp.minute = 30
        timestamp.second = 15
        self.assertEqual(timestamp.minute, 30)
        self.assertEqual(timestamp.second, 15)
        self.assertAssignRaises(ValueError, timestamp, "year", -1)
        self.assertAssignRaises(ValueError, timestamp, "month", 13)
        self.assertAssignRaises(ValueError, timestamp, "month", 0)
        self.assertAssignRaises(ValueError, timestamp, "day", 32)
        timestamp.month = 2
        self.assertAssignRaises(ValueError, timestamp, "day", 29)
        timestamp.month = 6
        self.assertAssignRaises(ValueError, timestamp, "day", 31)
        timestamp.month += 1
        timestamp.day = 31
        tzinfo = kml.UTCOffset(-5)
        timestamp.tzinfo = tzinfo
        timestamp.value = str(timestamp)
        self.cloneTest(timestamp, self.verbose)
        
    def testTimeSpan(self):
        begin = kml.TimeStamp("", 2010, 1, 1)
        end = kml.TimeStamp("", 2010, 12, 31)
        timespan = kml.TimeSpan("", begin, end)
        self.assertEqual(timespan.begin, begin)
        self.assertEqual(timespan.end, end)
        self.assertAssignSuccessful(timespan, "end", None)
        self.assertAssignSuccessful(timespan, "end", end)
        self.cloneTest(timespan, self.verbose)
        
    #--------------------------------------------------------------------------
    # Views
    #--------------------------------------------------------------------------
    
    def testLookAt(self):
        view = kml.LookAt("foo", longitude=1, latitude=2, altitude=3, 
                          tilt=45, heading=90, range=100000)
        self.assertEqual(view.longitude, 1)
        self.assertEqual(view.latitude, 2)
        self.assertEqual(view.altitude, 3)
        self.assertEqual(view.tilt, 45)
        self.assertEqual(view.heading, 90)
        self.assertEqual(view.range, 100000)
        view.altitude=3
        view.longitude=2
        view.latitude=1
        self.assertAssignRaises(ValueError, view, "latitude", 91)
        self.assertAssignRaises(ValueError, view, "longitude", -181)
        self.assertAssignRaises(TypeError, view, "latitude", "90 S")
        self.assertAssignRaises(TypeError, view, "longitude", "90 W")
        self.assertAssignRaises(TypeError, view, "altitude", "foo")
        self.assertAssignRaises(ValueError, view, "tilt", 181)
        self.assertAssignRaises(ValueError, view, "tilt", -1)
        self.assertAssignRaises(TypeError, view, "tilt", "foo")
        self.assertAssignRaises(ValueError, view, "heading", 361)
        self.assertAssignRaises(ValueError, view, "heading", -1)
        self.assertAssignRaises(TypeError, view, "heading", "foo")
        self.assertAssignRaises(TypeError, view, "range", "foo")
        self.cloneTest(view, self.verbose)
      
    def testCamera(self):
        view = kml.Camera("foo", coordinates=kml.Coordinate(1,2,3))
        self.assertEqual(view.longitude, 1)
        self.assertEqual(view.latitude, 2)
        self.assertEqual(view.altitude, 3)
        view = kml.Camera("foo", longitude=1, latitude=2, altitude=3, 
                          tilt=45, heading=90, roll=10)
        self.assertEqual(view.longitude, 1)
        self.assertEqual(view.latitude, 2)
        self.assertEqual(view.altitude, 3)
        view.altitude=3
        view.longitude=2
        view.latitude=1
        self.assertAssignRaises(ValueError, view, "latitude", 91)
        self.assertAssignRaises(ValueError, view, "longitude", -181)
        self.assertAssignRaises(TypeError, view, "latitude", "90 S")
        self.assertAssignRaises(TypeError, view, "longitude", "90 W")
        self.assertAssignRaises(TypeError, view, "altitude", "foo")
        self.assertAssignRaises(ValueError, view, "tilt", 181)
        self.assertAssignRaises(ValueError, view, "tilt", -1)
        self.assertAssignRaises(TypeError, view, "tilt", "foo")
        self.assertAssignRaises(ValueError, view, "heading", 361)
        self.assertAssignRaises(ValueError, view, "heading", -1)
        self.assertAssignRaises(TypeError, view, "heading", "foo")
        self.assertAssignRaises(ValueError, view, "roll", -181)
        self.assertAssignRaises(ValueError, view, "roll", 181)
        self.cloneTest(view, self.verbose)
        
    #--------------------------------------------------------------------------
    # Features
    #--------------------------------------------------------------------------
    
    def testPlacemark(self):
        point = kml.Point("foo", 1,2,3)
        view = kml.LookAt("foo", coordinates=kml.Coordinate(1,2,3), range=100000)
        placemark = kml.Placemark(id="foo", 
                                  geometry=point,
                                  name = "foobar",
                                  description = "Not very descriptive :-).",
                                  visibility = False,
                                  open = True,
                                  view = view)
        self.assertEqual(placemark.Geometry, point)
        self.assertEqual(placemark.View, view)
        self.assertEqual(placemark.visibility, False)
        self.assertEqual(placemark.open, True)
        self.assertEqual(placemark.address, "")
        self.assertEqual(placemark.name, "foobar")
        self.assertEqual(placemark.Snippet, None)
        snippet=kml.Snippet(value="Some snippet of information.")
        view = kml.LookAt("foo2", coordinates=kml.Coordinate(3,2,1), range=200000)
        self.assertAssignSuccessful(placemark, "Snippet", snippet)
        self.assertAssignSuccessful(placemark, "name", "newname")
        self.assertAssignSuccessful(placemark, "description", "Still not very descriptive.")
        self.assertAssignSuccessful(placemark, "visibility", True)
        self.assertAssignSuccessful(placemark, "open", False)
        self.assertAssignSuccessful(placemark, "view", view)
        self.cloneTest(placemark, self.verbose)
        
    def testDocument(self):
        document = kml.Document()
        document.name = "My Document"
        document.description = "Some description"
        #comments currently break cloneTest
        #document.comments = ["Foo"]
        view = kml.LookAt("foo", longitude=1, latitude=2, altitude=3, 
                          tilt=45, heading=90, range=100000)
        placemark = kml.Placemark(id="foo", 
                          geometry=kml.Point("", 1, 2, 3),
                          name = "foobar",
                          description = "Not very descriptive.",
                          visibility = False,
                          open = True,
                          view = kml.LookAt("bar", longitude=4, latitude=5, 
                                            altitude=6))
        iconStyle = kml.IconStyle()
        iconStyle.scale = 0.75
        iconStyle.heading = 0
        iconStyle.Icon = kml.Icon(href="1.png")
        iconStyle.color = 0xffffffff
        style = kml.Style()
        style.IconStyle = iconStyle
        document.addStyle(style)
        document.addChildFeature(placemark)
        self.assertEqual(placemark, document.children[0])
        document.View = view
        self.cloneTest(document, self.verbose)
        
    def testFolder(self):
        folder = kml.Folder()
        folder.name = "My Folder"
        folder.description = "Some description"
        #comments currently break cloneTest
        #folder.comments = ["Foo"]
        view = kml.LookAt("foo2", longitude=1, latitude=2, altitude=3, 
                          tilt=45, heading=90, range=100000)
        placem = kml.Placemark(id="foo", 
                          geometry=kml.Point("", 1, 2, 3),
                          name = "foobar",
                          description = "Not very descriptive.",
                          visibility = False,
                          open = True,
                          view = kml.LookAt("bar", longitude=4, latitude=5, 
                                            altitude=6))
        iconStyle = kml.IconStyle()
        iconStyle.scale = 0.75
        iconStyle.heading = 0
        iconStyle.Icon = kml.Icon(href="1.png")
        iconStyle.color = 0xffffffff
        style = kml.Style()
        style.IconStyle = iconStyle
        folder.addStyle(style)
        folder.addChildFeature(placem)
        self.assertEqual(placem, folder.children[0])
        folder.View = view
        self.cloneTest(folder, self.verbose)
        
    def testNetworkLink(self):
        networkLink = kml.NetworkLink()
        networkLink.flyToView = True
        networkLink.refreshVisibility = True
        networkLink.Link = "http://example.com/foo"
        self.assertEqual(networkLink.flyToView, True)
        self.assertEqual(networkLink.refreshVisibility, True)
        self.assertEqual(networkLink.Link.href, "http://example.com/foo")
        self.cloneTest(networkLink, self.verbose)
        
                       
    
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()